﻿using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Net;

namespace TaskScheduledGetOrderList
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (Convert.ToBoolean(ConfigurationManager.AppSettings["InitGetOrderListFlag"]))
                {
                    var startDate = Convert.ToDateTime(ConfigurationManager.AppSettings["InitGetOrderListDate"]);
                    var endDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));


                    while (startDate <= endDate)
                    {
                        log(
                            PostJSON(
                                string.Format("{0}?startDate={1}&endDate={2}",
                                    ConfigurationManager.AppSettings["CallUrl"], startDate.ToString("yyyy-MM-dd"),
                                    startDate.AddDays(1).ToString("yyyy-MM-dd")), string.Empty));

                        startDate = startDate.AddDays(1);
                    }
                }
                else
                {
                    System.Threading.Thread.Sleep(10000);
                    var startDate = Convert.ToDateTime(DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd"));
                    var endDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                    while (startDate <= endDate)
                    {
                        startDate = startDate.AddDays(1);
                        try
                        {
                            log(PostJSON(
                                string.Format("{0}?startDate={1}&endDate={2}",
                                    ConfigurationManager.AppSettings["CallUrl"], startDate.ToString("yyyy-MM-dd"),
                                    startDate.AddDays(1).ToString("yyyy-MM-dd")), string.Empty));
                        }
                        catch (Exception e)
                        {
                            log(string.Format("从{0}到{1}异常{2}", startDate, startDate.AddDays(1), e.Message + e.StackTrace));
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                log(ex.Message + ex.StackTrace);
            }
        }
        public static string PostJSON(string url, string body)
        {
            return PostData(url, body, "text/json");
        }

        public static string PostXml(string url, string body)
        {
            return PostData(url, body, "text/xml");
        }
        private static string PostData(string url, string body, string contentType)
        {
            string result;

            StreamWriter myWriter = null;
            var objRequest = (HttpWebRequest)WebRequest.Create(url);
            objRequest.Method = "POST";

            //objRequest.ContentLength = strPost.Length;
            objRequest.ContentType = contentType;//"text/json"; //提交xml 
            //objRequest.ContentType = "application/x-www-form-urlencoded";//提交表单
            //默认值是 100,000 毫秒（100 秒）。
            objRequest.Timeout = 3600000*24;
            try
            {
                myWriter = new StreamWriter(objRequest.GetRequestStream());
                myWriter.Write(body);
            }
            catch (Exception e)
            {
                return e.Message + e.StackTrace;
            }
            finally
            {
                if (myWriter != null) myWriter.Close();
            }

            var objResponse = (HttpWebResponse)objRequest.GetResponse();
            using (var sr = new StreamReader(objResponse.GetResponseStream()))
            {
                result = sr.ReadToEnd();
                sr.Close();
            }
            return result;
        }


        public static string GetUpLoadDirPath()
        {
            var folder = "File";
            string dirPath = AppDomain.CurrentDomain.BaseDirectory + folder;
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }
            //String ymd = DateTime.Now.ToString("yyyy", DateTimeFormatInfo.InvariantInfo);
            dirPath += "\\" + DateTime.Now.ToString("yyyy", DateTimeFormatInfo.InvariantInfo) + "\\";
            folder += "\\" + DateTime.Now.ToString("yyyy", DateTimeFormatInfo.InvariantInfo) + "\\";
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }
            dirPath += DateTime.Now.ToString("MM", DateTimeFormatInfo.InvariantInfo) + "\\";
            folder += DateTime.Now.ToString("MM", DateTimeFormatInfo.InvariantInfo) + "\\";
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }
            dirPath += DateTime.Now.ToString("dd", DateTimeFormatInfo.InvariantInfo) + "\\";
            folder += DateTime.Now.ToString("dd", DateTimeFormatInfo.InvariantInfo) + "\\";
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }
            return dirPath;
        }
        private static void log(string log_details)
        {
            try
            {

                StreamWriter sw = new StreamWriter(GetUpLoadDirPath() + "Log.log", true);
                sw.WriteLine(log_details);
                sw.Close();
            }
            catch (Exception err)
            {
                //io err
            }
        }
    }
}
